﻿#include "OperatorsGeometryImport.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/CentralWidget.h" 
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelImport.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"

#include <QFileDialog>
#include <QApplication>

namespace ModelOper
{

    bool OperatorsGeometryImport::execGUI()
    {
        //获取工作路径
        QString workDir = "";
        if (FITKAPP->getAppSettings())
            workDir = FITKAPP->getAppSettings()->getWorkingDir();
        if (workDir.isEmpty()) workDir = QApplication::applicationDirPath();
        //输入读入文件路径
        QString fileName = QFileDialog::getOpenFileName(FITKAPP->getGlobalData()->getMainWindow(), tr("Import Geometry"), workDir, tr("File(*.brep ; *.stp ; *.step ; *.igs ; *.stl)"));
        if (fileName.isEmpty())return false;
        //设置文件路径
        this->setArgs("FileName", fileName);
        return true;
    }

    bool OperatorsGeometryImport::execProfession()
    {
        //获取读入文件路径
        QString fileName;
        this->argValue<QString>("FileName",fileName);
        if (fileName.isEmpty()) return false;

        Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        if (geometryData == nullptr) return false;
        Interface::FITKInterfaceGeometryFactory* geoFactory = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (geoFactory == nullptr)return false;
        //创建导入的几何数据对象
        Interface::FITKAbsGeoModelImport* geoObj = geoFactory->createCommandT<Interface::FITKAbsGeoModelImport>(Interface::FITKGeoEnum::FITKGeometryComType::FGTImport);
        if (geoObj == nullptr)return false;
        geoObj->setFileName(fileName);
        //设置几何数据对象名称
        if (geoObj->getDataObjectName().isEmpty()) {
            QFileInfo fileInfo(fileName);
            // 获取文件名称（不包含路径与文件类型）
            QString name = fileInfo.baseName();
            geoObj->setDataObjectName(name);
        }
        //更新几何数据
        bool result = geoObj->update();
        if (result)
        {
            //将几何数据存放在管理器
            geometryData->appendDataObj(geoObj);
            AppFrame::FITKMessageNormal(QString("Import Geometry File Succeed !!! %1").arg(fileName));
            //获取三维数据操作器，更新几何数据到渲染层
            EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
            if (graphOper == nullptr)return false;
            graphOper->updateGraph(geoObj->getDataObjectID());
            //保存执行的脚本
            QString scr = QString("Global.Files.ImportGeoFile('%1')").arg(fileName);
            this->saveScript(scr);
        }
        else
        {
            //释放空间
            delete geoObj;
            AppFrame::FITKMessageError(QString("Import Geometry File Err !!! %1").arg(fileName));
        }
        
        this->clearArgs();
        return true;
    }

    void OperatorsGeometryImport::setSEASubSystemNodes(int objID)
    {
        //获取几何数据
        Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        if (geometryData == nullptr) return;
        Interface::FITKAbsGeoCommand* geoData = geometryData->getDataByID(objID);
        if (!geoData)return;
        Interface::FITKVirtualTopoManager* virTopoMgr = geoData->getVirtualTopoManager();
        if (!virTopoMgr)return;
        //获取点的虚拓扑形状管理器
        Interface::FITKShapeVirtualTopoManager* virPointMgr = virTopoMgr->getShapeVirtualTopoManager(Interface::FITKGeoEnum::VTopoShapeType::VSPoint);
        if (!virPointMgr) return;
        //创建几何点查询工具
        Interface::FITKInterfaceGeometryFactory* geoFac = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (!geoFac) return;
        Interface::FITKAbstractGeomToolsCreator* geoTools = geoFac->getGeomToolsCreator();
        if (!geoTools)return;
        Interface::FITKAbstractGeomPointTool* geoPointTool = geoTools->createPointTool();
        if (!geoPointTool)return;
        //获取点的数据-OCC接口
        int count = virPointMgr->getDataCount();
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return;
        Acoustics::FITKAcousticsDiscNodeList* nodesData = discModel->getNodes();
        if (!nodesData) return;
        //设置点的数据
        for (int i = 0; i < count; ++i)
        {
            int pID = virPointMgr->getIDByIndex(i);
            double coor[3]{ 0, 0, 0 };
            if (!geoPointTool->getXYZ(pID, coor)) continue;
            Acoustics::DiscNode* node = nodesData->addDiscNode(coor[0], coor[1], coor[2]);
            if (!node)return;
            node->setNodeSource(Acoustics::DiscNodeSourceType::DiscNodeSource::Geom);
            node->setSourceID(objID);
            node->setSourceNodeID(pID);
        }
        //使用完几何点工具需要释放工具对象
        delete geoPointTool;
    }
}

